package com.sanjiahantang.youjiarole.module.me.address.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiarole.module.me.address.presenters.AddressPresenter;
import com.sanjiahantang.youjiarole.module.me.address.views.AddressView;


/**
 * Created by zy on 2017/9/21.
 *
 * 收货地址管理
 */
public class AddressListActivity extends BaseMvpActivity<AddressView,AddressPresenter> implements View.OnClickListener,
        AdapterView.OnItemClickListener,AddressView {

    private TextView title;
    private ImageView back;
    private ListView listView;


    private boolean flag;

    @Override
    public AddressPresenter initPresenter() {
        return new AddressPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_addresslist);
        flag = getIntent().getBooleanExtra(KEY_ORDER,false);


        title = (TextView)findViewById(R.id.title);
        back = (ImageView)findViewById(R.id.back);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        back.setBackgroundResource(R.drawable.i_back_black);

        findViewById(R.id.btnAdd).setOnClickListener(this);
        listView = (ListView)findViewById(R.id.listView);

        if(flag){
            title.setText("确认订单");
            listView.setOnItemClickListener(this);
        }else {
            title.setText("收货地址");
        }






    }

    @Override
    public void initData() {
        presenter.initAdapter($this,new OnLoadListener(){
            @Override
            public void initList(QuickAdapter adapter) {
                listView.setAdapter(adapter);
            }
        });
    }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent();
        AddressBean.Data item = (AddressBean.Data) adapterView.getItemAtPosition(i);
        intent.putExtra(AddressView.KEY_NAME,item.getCustomerName());
        intent.putExtra(AddressView.KEY_ADDRESS,item.getFullAddress());
        intent.putExtra(AddressView.KEY_PHONE,item.getCellPhone());
        intent.putExtra(AddressView.KEY_ID,item.getUcaId());
        setResult(Activity.RESULT_OK,intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
            //新建地址
            case R.id.btnAdd:
                presenter.addNewAddress($this,listView.getChildCount());
                break;
        }
    }

    @Override
    protected void onPostResume() {
        presenter.onPostResume($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof Integer){
                    int size = (int) content;
                    if (flag && size == 0){
                        presenter.addNewAddress($this,0);
                    }
                }
            }
        });
        super.onPostResume();
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (0xaa == requestCode && resultCode == RESULT_CANCELED){
            finish();
        }
    }
}
